<link type="text/css" href="/admins/js/pagerView/pager-view.css" rel="stylesheet" />
<link type="text/css" href="/admins/js/jquery/css/ui-lightness/jquery-ui-1.8.20.custom.css" rel="stylesheet" />
<script type="text/javascript" src="/admins/js/jquery/jquery-ui-1.8.20.custom.min.js"></script>
<script type="text/javascript" src="/admins/js/jquery/jquery.ui.datepicker-zh-CN.js"></script>
<script type="text/javascript" src="/admins/js/pagerView/pager-view.js"></script>

<script src="http://l.tbcdn.cn/apps/top/x/sdk.js?appkey=21170405"></script>

<?php
$app_key = '21170405';
$secret = 'e7b2c2be477fcd7196e40c1f8b81a0ee';
$timestamp = time() . "000";
$message = $secret . 'app_key' . $app_key . 'timestamp' . $timestamp . $secret;
$mysign = strtoupper(hash_hmac("md5", $message, $secret));
setcookie("timestamp", $timestamp);
setcookie("sign", $mysign);
?> 

<!--  未实现完成  -->
<script type="text/javascript">
    $().ready(function(){
        //console.log(TOP);
        TOP.api('rest', 'get',{
            method:'taobao.taobaoke.widget.url.convert',
            url:'http://detail.tmall.com/item.htm?spm=a230r.1.10.237.oFnHzk&id=18614068688'
        },function(resp){
            if(resp.error_response){
                alert('taobao.taobaoke.widget.url.convert接口获取商信息品失败!'+resp.error_response.msg);
                return false;
            }
            //var respItem=resp.taobaoke_items.taobaoke_item;
            //alert(respItem.length);
            return;
            
        })
    });
</script>
<!--  未实现完成  -->



<script type="text/javascript">
    $(function(){
    
        $.datepicker.setDefaults( $.datepicker.regional[ "zh-CN" ] );
        $( "#form1 #sel #starttime" ).datepicker({
            changeMonth: true,
            changeYear: true
        });
        $( "#form1 #sel #endtime" ).datepicker({
            changeMonth: true,
            changeYear: true
        });
    });
</script>



<!-- 弹出div CSS -->
<style type="text/css">
    <!--
    .mydiv {
        background-color: #FFFFFF;
        border: 1px solid #000000;
        text-align: center;
        line-height: 40px;
        font-size: 12px;
        z-index:999;/*优先级*/
        width: 400px;
        height: 50px;
        left:50%;
        top:50%;
        margin-left:-150px!important;/*FF IE7 该值为本身宽的一半 ，!important的出现就是为了让用户自己设置被执行语句的优先级*/
        margin-top:-60px!important;/*FF IE7 该值为本身高的一半*/
        margin-top:0px;
        position:fixed!important;/* FF IE7*/
        position:absolute;/*IE6*/
        _top:       expression(eval(document.compatMode &&
            document.compatMode=='CSS1Compat') ?
            documentElement.scrollTop + (document.documentElement.clientHeight-this.offsetHeight)/2 :/*IE6*/
            document.body.scrollTop + (document.body.clientHeight - this.clientHeight)/2);/*IE5 IE5.5*/
    }
    .bg,.popIframe {

    }
    .popIframe {
        filter:alpha(opacity=0);/*IE 设置透明度*/
        opacity:0;/*FF*/
    }
    -->
</style>
<!-- 弹出div JS -->
<script language="javascript" type="text/javascript">
    function showDiv(){
        document.getElementById('popDiv').style.display='block';
        document.getElementById('popIframe').style.display='block';
        document.getElementById('bg').style.display='block';
    }
    function closeDiv(){
        document.getElementById('popDiv').style.display='none';
        document.getElementById('bg').style.display='none';
        document.getElementById('popIframe').style.display='none';
    }
</script>

<style>
    .datatable {
        width:90%;
        text-align: left;
        font-size: 12px;
        font-family: verdana;
        background: #c0c0c0; margin:2px;
    }

    .datatable thead tr,

    .datatable tfoot tr {
        background: #c0c0c0;
    }

    .datatable tbody tr {
        background: #f0f0f0;
        height: 30px;
    }

    .dataTable span{
        margin-left: 20px;
        font-size: 14px;
        vertical-align: middle;
    }
    #waybill-table td,#waybill-table th{
        border: 1px solid #000; 
        height:28px;
    }
    .datatable td {background-color:white;}

    .datatable .hover td {background-color:#99CCFF;}

    .datatable .select td {background-color:#EEEE99;}

    #ui-datepicker-div{ font-size:12px;}
</style>
<div style="padding:2px; margin:2px;" class="dataTable">
    <form action="/managebuyshop/index/stus/<?php echo $this->status; ?>"  method="get" style="margin:0px" id="form1">
        <div style=" border:1px solid #DDDDDD; padding:2px; margin:2px;line-height: 36px;" id='sel'> 
            <div>
                <label for="orderno">代购单号：</label><input type="text" id="orderno" name="orderno" value="<?php echo $this->orderno; ?>"/>
                <label for="productName">商品名称：</label><input type="text" id="productName" name="productName" value="<?php echo $this->productName; ?>"/>
                <label for="userid">用户ID：</label><input type="text" id="userid" name="userid" value="<?php echo $this->userid; ?>" />
            </div>
            <div>
                <label for="procureNo">淘宝单号：</label><input type="text" id="procureNo" name="procureNo" value="<?php echo $this->procureNo; ?>"/>
                <label for="logisticsNo">物流单号：</label><input type="text" id="logisticsNo" name="logisticsNo" value="<?php echo $this->logisticsNo; ?>"/>
                <label for="storeno">仓库号：</label><input type="text" id="storeno" name="storeno" value="<?php echo $this->storeno; ?>" />
            </div>
            <div>
                <label for="name">用户名&nbsp;&nbsp;&nbsp;：</label><input type="text" id="username" name="username" value="<?php echo $this->username; ?>"/>
                <label for="starttime">提交时间：</label><input type="text" id="starttime" name="starttime" value="<?php echo $this->starttime; ?>" />
                <label for="endtime">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;--&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label><input type="text" id="endtime" name="endtime" value="<?php echo $this->endtime; ?>"  />

                <input type="submit" id="queryButton" value="查询"/>
            </div>
        </div>

    </form>

    <div id="shipInfo" style="display:none;width: 560px">
        物流信息加载中,请稍候...
    </div>
    <table cellspacing="1" width="100%" border="1" class="datatable" id="waybill-table">
        <thead>
            <tr>
                <th class="w0">宝贝信息</th>
                <th class="w0">&nbsp;</th>
                <th class="w1">单价</th>
                <th class="w1">数量</th>
                <th class="w0">实付款</th>
                <th class="w1">状态</th>
            </tr>
        </thead>
        <tbody >

            <?php
            if ($this->buydetail != '') {
                foreach ($this->buydetail as $Item) {
                    $shopdetail = $Item->shipdetail;
                    ?>

                    <tr>
                        <td colspan="5" style="background-color: #DEF0FB;height: 20px;">
                            <span>订单编号：<?php echo $Item->orderid; ?></span>
                            <span>时间：<?php echo $Item->dateCreated; ?></span>
                            <span>用户ID：<?php echo $Item->userId; ?></span>
                            <span>仓库号：<a href="/manageuser/storedetail/id/<?php echo $Item->userId; ?>" target="_blank" ><?php echo $Item->byuserdetail->storeno; ?></a></span>
                            <span>昵称：<?php echo $Item->user->nikename; ?></span>

                            <span id="ShowOrHide<?php echo $Item->Id ?>" onclick="ShowOrHideProduct(<?php echo $Item->Id; ?>,<?php echo count($shopdetail); ?>)" style="cursor:pointer">收起</span>
                        </td>
                        <td style="background-color: #DEF0FB;height: 20px;" align="center">
                            <?php
                            switch ($Item->status) {
                                case Application_Model_Order::STATUS_PAID:
                                    $restatus = Application_Model_Order::STATUS_CONFRIM;
                                    break;
                                case Application_Model_Order::STATUS_CONFRIM:
                                    $restatus = Application_Model_Order::STATUS_UNSEND;
                                    break;
                                case Application_Model_Order::STATUS_UNSEND:
                                    $restatus = Application_Model_Order::STATUS_SELLER_SENDED;
                                    break;
                                case Application_Model_Order::STATUS_SELLER_SENDED:
                                    $restatus = Application_Model_Order::STATUS_INSTORE;
                                    break;
                                case Application_Model_Order::STATUS_INSTORE:
                                    $restatus = Application_Model_Order::STATUS_CANCEL;
                                    break;
                                default :
                                    $restatus = 999;
                                    break;
                            }
                            if ($restatus != 999) {
                                ?>
                                <a href="javascript:UpdateStatu(<?php echo $Item->id . "," . $restatus; ?>)" >
                                    <div id="OperTitle<?php echo $Item->id ?>">
                                        <?php echo '设为' . Application_model_order::getStatusDescription($restatus); ?>
                                    </div>
                                </a>
                            <?php } ?>
                        </td>
                    </tr>


                    <?php echo "<tr id='tr_" . $Item->Id . "_1'>";
                    $index = 2; ?>
                    <?php
                    if ($shopdetail != null) {
                        foreach ($shopdetail as $key => $val) {
                            if (count($shopdetail) > 1) {
                                if ($key > 0) {
                                    echo "</tr><tr id='tr_" . $Item->Id . "_$index'>";
                                    $index+=1;
                                }
                            }
                            $productd = $val->byproductd;
                            ?>
                        <td valign="middle" class="w0">
                            <div style="float:left;">
                                <a href="<?php echo $productd->url; ?>" target="_blank"><img src="<?php echo $productd->remoteImage; ?>" width="50px" height="50px" style="vertical-align:middle;"/></a>
                            </div>
                            <div style="float:left;color:#00f;">
                                &nbsp;&nbsp;<a href="<?php
                            //先检查是否有做淘宝推广，如有，则显示推广URL，没有则显示原来的url
                            //echo $productd->url;
                            echo Application_Model_OrderItem::GetTopUrl($productd, $val);
                            ?>" target="_blank"><?php echo iconv_strlen($val->productName) >= 50 ? mb_substr($val->productName, 0, 50, 'utf-8') . '...' : $val->productName; ?></a>
                            </div><br>
                            <div style="float:left;color:#666;">&nbsp;&nbsp;<a href="<?php echo $productd->url ?>" target="_blank"><?php echo iconv_strlen($productd->PropertyAlias) >= 50 ? mb_substr($productd->PropertyAlias, 0, 50, 'utf-8') . '...' : $productd->PropertyAlias; ?></a></div>
                        </td>

                        <td class="w1" align="center"><a href="javascript:AddRemarks(<?php echo $val->id; ?>);" ><div id="Comment<?php echo $val->id; ?>">添加说明</div></a></td>

                        <td class="w1" align="center"><?php echo $val->price; ?>
                            <?php
                            if ($val->status != 1) {
                                if ($Item->status != 10) {
                                    ?>
                                    <a href="javascript:SingleRetuenMoney(<?php echo $val->id; ?>,<?php echo $Item->userId; ?>);" >
                                        <div id="SingleRetuenMoney<?php echo $val->id; ?>">单项退款</div>
                                        <?php
                                    }
                                } else {
                                    ?>
                                    <div><font color="green">已退款</font></div>
                                    <?php
                                }
                                ?>
                            </a>
                        </td>
                        <td class="w0" align="center"><?php echo $val->quantity; ?></td>
                <?php if ($key == 0) { ?>
                            <td class="w1" rowspan="<?php echo count($Item->shipdetail); ?>" align="center">￥<?php echo $Item->sum; ?> <?php if ((int) ($Item->shipFee) > 0) {
                        echo "<br><font color='grey'>含运费：" . $Item->shipFee . "</font>";
                    } ?></td>
                            <td class="w0" rowspan="<?php echo count($Item->shipdetail); ?>"align="center"><div id="statesname<?php echo $Item->id ?>"><?php echo $Item->StatusText; ?></div>
                                <?php
                                if ($Item->status != 11) {
                                    if ($Item->status != 10) {
                                        ?>
                                        <a href="javascript:ReturnMoney(<?php echo $Item->id; ?>,<?php echo $Item->userId ?>)" >取消并退款</a>
                                <?php }
                            } else { ?>

                    <?php } ?>
                            </td>

                <?php }
            }
        } ?>
                </tr>

                <tr>
                    <td colspan="6" style="background-color: #FFFFFF;height: 20px;">
                        淘宝订单编号：<input type="textbox" id="procureNo<?php echo $Item->id ?>" name="procureNo<?php echo $Item->id ?>" value="<?php echo $Item->procureNo; ?>" /> 物流单号：
                        <select name="logistics<?php echo $Item->id ?>" id="logistics<?php echo $Item->id ?>">
        <?php foreach ($this->expresscomp as $expresscomp) { ?>
                                <option value="<?php echo $expresscomp->id; ?>" <?php if ($expresscomp->id == $Item->logistics) echo "selected" ?>><?php echo $expresscomp->word . '_' . $expresscomp->name; ?></option>
                        <?php } ?>
                        </select>
                        <input type="logisticsNo" id="logisticsNo<?php echo $Item->id ?>" name="logisticsNo<?php echo $Item->id ?>" value="<?php echo $Item->logisticsNo; ?>" /> <input type="button" id="btnLogistics<?php echo $Item->id ?>" name="btnLogistics<?php echo $Item->id ?>" onclick="javascript:UpdateLogistics(<?php echo $Item->id ?>)" value="更新" />


                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                        <a href="<?php
                   if ($Item->procureNo)
                       echo 'http://trade.taobao.com/trade/detail/trade_item_detail.htm?spm=a1z09.2.9.27.yegbD7&bizOrderId=' . $Item->procureNo;
                        ?>" target="_blank">跟踪订单</a>
                    </td>
                </tr>
                <tr>
                    <td colspan="6">&nbsp;</td>
                </tr>

            <?php
        }
    }
    ?>
        </tbody>
    </table>

<?php
echo $this->partial('paging.phtml', array("pages" => $this->result, "pamas" => "username=" . $this->username . "&storeno=$this->storeno&dateselect=$this->dateselect&starttime=$this->starttime&endtime=$this->endtime&status=$this->status"));
?>
</div>


<div id="popDiv" class="mydiv" style="display:none;">
    <table width="100%" height="100%" border="0">
        <tr height="100%">
            <td nowrap width="70px">&nbsp;添加说明：</td>
            <td width="320px" align="left" nowrap style="padding-right: 5px">
                <input type="hidden" id="hidCommentId" />            
                <input type="textbox" id="txtComment" name="txtComment" style="width:250px"></input>
                <input type="button" id="btnSubmitComment" value="提交" onclick="SubmitComment()"/>
                <input type="button" id="btnSubmitComment" value="关闭" onclick="closeDiv()"/>
            </td>
        </tr>

        </td>
        </tr>
        <tr heihght="10px">
            <td colspan="2" valign="top" align="right">&nbsp;</td>
        </tr>
    </table>
</div>
<div id='popIframe' class='popIframe' frameborder='0' ></div>
</body>

<script type="text/javascript">
    $('#waybill-table #accept').on('click',function(){
        var id = $(this).attr('name');
        if(id == ''){
            alert("请选择订单");
            return false;
        }else{
            $.ajax({
                type: 'POST',
                url: '/managebuyshop/accept/id/'+id,
                success: function(data) {
                    if(data == 1){
                        alert("受理成功");
                    }else if(data == 2){
                        alert("受理只接受已付款的订单");
                    }else{
                        alert("发生未知错误，请重新操作");
                    }
                    location.href='/managebuyshop/index';
                }
            });
            
        }
    });
    $('#waybill-table #buyshop').on('click',function(){
        var id = $(this).attr('name');
        if(id == ''){
            alert("请选择订单");
            return false;
        }else{
            location.href='/managebuyshop/buyshop/id/'+id;            
        }
    });
    $('#waybill-table #instore').on('click',function(){
        var id = $(this).attr('name');
        if(id == ''){
            alert("请选择订单");
            return false;
        }else{
            location.href='/managestore/instore/id/'+id;            
        }
    });
    $('#waybill-table #shopout').on('click',function(){
        var id = $(this).attr('name');
        if(id == ''){
            alert("请选择订单");
            return false;
        }else{
            location.href='/managebuyshop/shopout/id/'+id;            
        }
    });
    
    
    
    
    
    
    
    
    
    
    
    
    
    /**
     *商品收起、展开
     **/
    function ShowOrHideProduct(orderId, trId)
    {        
        for(var i=1; i<=trId; i++)
        {
            if(document.getElementById("tr_"+orderId+"_"+i).style.display == "none")
            {
                $("#tr_"+orderId+"_"+i).show();
                $("#ShowOrHide"+orderId).html("收起");
                $("OrderPrice"+orderId).attr("rowspan","0");
            }
            else
            {
                $("#tr_"+orderId+"_"+i).hide();
                $("#ShowOrHide"+orderId).html("展开");
                $("OrderPrice"+orderId).attr("rowspan",trId);
            }                
        }        
    }
    
    /**
     *提交说明
     */
    function SubmitComment()
    {
        var comment = $("#txtComment").val();
        var id = $("#hidCommentId").val();
        if(comment == '')
        {
            alert("请输入说明信息.");
            return;
        }
        
        $("#btnSubmitComment").html('提交中...');
        $.ajax({
            type: 'POST',
            url: '/managebuyshop/updateorderitemcomment/id/'+id+'/comment/'+comment,
            success: function(data) {
                if(data == 1){
                    alert("提交成功");
                    $("#btnSubmitComment").html('提交');
                    closeDiv();
                }else{
                    alert("发生未知错误，请重新操作");
                }
            }
        });
    }
    
    /**
     *添加说明
     */
    function AddRemarks(id)
    { 
        $("#hidCommentId").val(id);
        
        $.ajax({
            type: 'POST',
            url: '/managebuyshop/getcomment/id/'+id,
            success: function(data) {
                $("#txtComment").val(data);
            }
        });
            
        showDiv();
    }
    
    /**
     *更改状态
     */
    function UpdateStatu(orderId,statu)
    {
        // var statu = <?php echo $this->stype ?>;
        var restatus="";
        $("#OperTitle"+orderId).html('设置中...');
        $.ajax({
            type: 'POST',
            url: '/managebuyshop/updatestatu/orderId/'+orderId+'/statu/'+statu,
            success: function(data) {
                if(data == 1){
                    switch (statu) {
                        case <?php echo Application_Model_Order::STATUS_PAID; ?>:
                                restatus = <?php echo Application_Model_Order::STATUS_CONFRIM; ?>;
                            $("#OperTitle"+orderId).html("<?php echo '设为' . Application_model_order::getStatusDescription(Application_Model_Order::STATUS_CONFRIM); ?>");
                            $("#statesname"+orderId).html("");
                            break;
                        case <?php echo Application_Model_Order::STATUS_CONFRIM; ?>:
                                restatus = <?php echo Application_Model_Order::STATUS_UNSEND; ?>;
                            $("#OperTitle"+orderId).html("<?php echo '设为' . Application_model_order::getStatusDescription(Application_Model_Order::STATUS_UNSEND); ?>");
                            $("#statesname"+orderId).html("已受理");
                            break;
                        case <?php echo Application_Model_Order::STATUS_UNSEND; ?>:
                                restatus = <?php echo Application_Model_Order::STATUS_SELLER_SENDED; ?>;
                            $("#OperTitle"+orderId).html("<?php echo '设为' . Application_model_order::getStatusDescription(Application_Model_Order::STATUS_SELLER_SENDED); ?>");
                            $("#statesname"+orderId).html("等待卖家发货");
                            break;
                        case <?php echo Application_Model_Order::STATUS_SELLER_SENDED; ?>:
                                restatus = <?php echo Application_Model_Order::STATUS_INSTORE; ?>;
                            $("#OperTitle"+orderId).html("<?php echo '设为' . Application_model_order::getStatusDescription(Application_Model_Order::STATUS_INSTORE); ?>");
                            $("#statesname"+orderId).html("卖家已发货");
                            break;
                        case <?php echo Application_Model_Order::STATUS_INSTORE; ?>:
                                restatus = <?php echo Application_Model_Order::STATUS_CANCEL; ?>;
                            $("#OperTitle"+orderId).html("<?php echo '设为' . Application_model_order::getStatusDescription(Application_Model_Order::STATUS_CANCEL); ?>");
                            $("#statesname"+orderId).html("已入库");
                            break;
                        default :
                            restatus = 999;
                            $("#OperTitle"+orderId).html("");
                            $("#statesname"+orderId).html("已取消");
                            break;
                        }
                        
                        alert("设置成功");
                    }else{
                        alert("发生未知错误，请重新操作");
                    }
                    //location.href='/managebuyshop/index/stus/<?php echo $this->status ?>';
                }
            });        
        }
    
        /**
         *更改物流信息
         */
        function UpdateLogistics(id)
        {
            var procureNo = $("#procureNo"+id).val();
            var logistics = $("#logistics"+id).val();
            var logisticsNo = $("#logisticsNo"+id).val();
        
            $("#btnLogistics"+id).val('更新中...');
            $.ajax({
                type: 'POST',
                url: '/managebuyshop/updatelogistics/id/'+id+'/procureNo/'+procureNo+'/logistics/'+logistics+'/logisticsNo/'+logisticsNo,
                success: function(data) {
                    if(data == 1){
                        alert("设置成功");
                        $("#btnLogistics"+id).val('更新成功');
                        $("#procureNo"+id).val(procureNo);
                        $("#logistics"+id+" option[value='"+logistics+"']").attr("selected", "selected");
                        $("#logisticsNo"+id).val(logisticsNo);
                    }else if(data == 2){
                        alert("物流单号已存在");
                        $("#btnLogistics"+id).val('更新');
                    }else{
                        alert("发生未知错误，请重新操作");
                    }
                    //location.href='/managebuyshop/index/stus/<?php echo $this->status ?>';
                }
            });
        }
    
        /**
         * 单个商品退款
         */
        function SingleRetuenMoney(id, userid)
        {
            $.ajax({
                type: 'POST',
                url: '/managebuyshop/singleretuenmoney/id/'+id+'/userid/'+userid,
                success: function(data) {
                    if(data == 1){
                        alert("退款成功");   
                    }else{
                        alert("发生未知错误，请重新操作");
                    }
                    location.href='/managebuyshop/index/stus/<?php echo $this->status ?>';
                }                
            });
        }
    
        /**
         * 整个订单退款
         */
        function ReturnMoney(id, userid)
        {
            $.ajax({
                type: 'POST',
                url: '/managebuyshop/retuenmoney/id/'+id+'/userid/'+userid,
                success: function(data) {
                    if(data == 1){
                        alert("退款成功");   
                    }else{
                        alert("发生未知错误，请重新操作");
                    }
                    location.href='/managebuyshop/index/stus/<?php echo $this->status ?>';
                }                
            });
        }
    
    
    
</script>
